package cn.terminus.web.customer.contractrentclause.controller;

import cn.terminus.web.customer.contractrentclause.form.ContractRentClauseNewForm;
import cn.terminus.web.customer.contractrentclause.form.ContractRentClauseEditForm;
import cn.terminus.web.customer.contractrentclause.form.ContractRentClauseQueryForm;
import cn.terminus.web.customer.contractrentclause.form.ContractRentClauseDeleteForm;
import cn.terminus.web.customer.contractrentclause.form.ContractRentClauseGetsForm;
import cn.terminus.client.customer.contractrentclause.param.ContractRentClauseNewParam;
import cn.terminus.client.customer.contractrentclause.param.ContractRentClauseEditParam;
import cn.terminus.client.customer.contractrentclause.param.ContractRentClauseQueryParam;
import cn.terminus.client.customer.contractrentclause.param.ContractRentClauseDeleteParam;
import cn.terminus.client.customer.contractrentclause.param.ContractRentClauseGetsParam;
import cn.terminus.client.customer.contractrentclause.result.ContractRentClauseResult;
import cn.terminus.client.customer.contractrentclause.service.ContractRentClauseService;
import cn.terminus.result.common.result.TerminusResult;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import cn.terminus.web.customer.common.AbstractTerminusController;
import cn.terminus.result.common.exception.DataAccessException;
import cn.terminus.result.common.exception.ServiceException;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import cn.terminus.result.common.util.BeanCopyUtils;
import cn.terminus.result.common.context.TerminusCallContext;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import java.util.List;

/**
 * 合同租金条款(ContractRentClause)控制层
 *
 * @author makejava
 * @version 1.0.0
 * @apiDefine ContractRentClause 合同租金条款
 * @since 2021-07-21 11:01:12
 */
@RestController
// todo 请修改path
@RequestMapping("/contractRentClause")
@Api(value = "合同租金条款(ContractRentClause)控制层")
public class ContractRentClauseController extends AbstractTerminusController {
    /**
     * 服务对象
     */
    @Resource
    private ContractRentClauseService contractRentClauseService;

    /**
     * @api {POST} /contractRentClause/add 合同租金条款新增
     * @apiVersion 1.0.0
     * @apiGroup ContractRentClause
     * @apiName 合同租金条款新增
     * @apiDescription 合同租金条款新增
     * @apiParam (RequestBody) {Long} contractId  合同ID
     * @apiParam (RequestBody) {Object} clauseBeginTime  条款开始时间
     * @apiParam (RequestBody) {Object} clauseEndTime  条款结束时间
     * @apiParam (RequestBody) {Double} rentalPrice  租金价格
     * @apiParam (RequestBody) {String} rentalUnitCode  租金单位code
     * @apiParam (RequestBody) {String} payBehaviorCode  付款时间code 延后 提前
     * @apiParam (RequestBody) {Integer} payDayNumber  付款天数
     * @apiParam (RequestBody) {String} payDayTypeCode  付款时间类型 工作日，自然日，指定几号
     * @apiParam (RequestBody) {String} chargingTypeCode  计费类型 按月，按实际天数
     * @apiParam (RequestBody) {String} unitPrice_RoleCode  单价换算规则
     * @apiParam (RequestBody) {String} conversionDayCode  换算天数
     * @apiParam (RequestBody) {Integer} payCycle  付款周期
     * @apiParam (RequestBody) {String} leaseTermDivisionCode  租期划分方式
     * @apiParam (RequestBody) {Integer} status  状态 1正常 2删除
     * @apiSuccess (Success 200) {String} code    状态码（00000000表示请求成功）
     * @apiSuccess (Success 200) {Boolean} success    成功
     * @apiSuccess (Success 200) {String} message    结果消息
     * @apiSuccess (Success 200) {String} data    结果
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":"[]"}
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "合同租金条款新增", httpMethod = "POST")
    public TerminusResult<String> add(@RequestBody ContractRentClauseNewForm form) {
        ContractRentClauseNewParam param = BeanCopyUtils.copyProperties(form, ContractRentClauseNewParam.class);
        try {
            return contractRentClauseService.add(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("新增合同租金条款失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("新增合同租金条款失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }


    /**
     * @api {POST} /contractRentClause/edit 合同租金条款编辑
     * @apiVersion 1.0.0
     * @apiGroup ContractRentClause
     * @apiName 合同租金条款编辑
     * @apiDescription 合同租金条款编辑
     * @apiParam (RequestBody) {Long} id  ID
     * @apiParam (RequestBody) {Long} contractId  合同ID
     * @apiParam (RequestBody) {Object} clauseBeginTime  条款开始时间
     * @apiParam (RequestBody) {Object} clauseEndTime  条款结束时间
     * @apiParam (RequestBody) {Double} rentalPrice  租金价格
     * @apiParam (RequestBody) {String} rentalUnitCode  租金单位code
     * @apiParam (RequestBody) {String} payBehaviorCode  付款时间code 延后 提前
     * @apiParam (RequestBody) {Integer} payDayNumber  付款天数
     * @apiParam (RequestBody) {String} payDayTypeCode  付款时间类型 工作日，自然日，指定几号
     * @apiParam (RequestBody) {String} chargingTypeCode  计费类型 按月，按实际天数
     * @apiParam (RequestBody) {String} unitPrice_RoleCode  单价换算规则
     * @apiParam (RequestBody) {String} conversionDayCode  换算天数
     * @apiParam (RequestBody) {Integer} payCycle  付款周期
     * @apiParam (RequestBody) {String} leaseTermDivisionCode  租期划分方式
     * @apiParam (RequestBody) {Integer} status  状态 1正常 2删除
     * @apiSuccess (Success 200) {String} code    状态码（00000000表示请求成功）
     * @apiSuccess (Success 200) {Boolean} success    成功
     * @apiSuccess (Success 200) {String} message    结果消息
     * @apiSuccess (Success 200) {Object} data    结果
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":true}
     */

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "合同租金条款编辑", httpMethod = "POST")
    public TerminusResult<Boolean> edit(@RequestBody ContractRentClauseEditForm form) {
        ContractRentClauseEditParam param = BeanCopyUtils.copyProperties(form, ContractRentClauseEditParam.class);
        try {
            return contractRentClauseService.update(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("修改合同租金条款失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("修改合同租金条款失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    /**
     * @api {POST} /contractRentClause/delete 合同租金条款删除
     * @apiVersion 1.0.0
     * @apiGroup ContractRentClause
     * @apiName 合同租金条款删除
     * @apiDescription 合同租金条款删除
     * @apiParam (RequestBody) {String[]} ids  合同租金条款ids
     * @apiSuccess (Success 200) {Object} data    结果
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":true}
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation(value = "合同租金条款删除", httpMethod = "POST")
    public TerminusResult<Boolean> delete(@RequestBody ContractRentClauseDeleteForm form) {
        ContractRentClauseDeleteParam param = BeanCopyUtils.copyProperties(form, ContractRentClauseDeleteParam.class);
        try {
            return contractRentClauseService.delete(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("删除合同租金条款失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("删除合同租金条款失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    /**
     * @api {POST} /contractRentClause/gets 合同租金条款详情
     * @apiVersion 1.0.0
     * @apiGroup ContractRentClause
     * @apiName 合同租金条款详情
     * @apiDescription 合同租金条款详情
     * @apiParam (RequestBody) {String[]} ids  合同租金条款ids
     * @apiSuccess (Success 200) {Object} data    结果
     * @apiSuccess (Success 200) {Object[]} data.datas    合同租金条款列表
     * @apiSuccess (Success 200) {Long} data.datas.id  ID
     * @apiSuccess (Success 200) {Long} data.datas.contractId  合同ID
     * @apiSuccess (Success 200) {Object} data.datas.clauseBeginTime  条款开始时间
     * @apiSuccess (Success 200) {Object} data.datas.clauseEndTime  条款结束时间
     * @apiSuccess (Success 200) {Double} data.datas.rentalPrice  租金价格
     * @apiSuccess (Success 200) {String} data.datas.rentalUnitCode  租金单位code
     * @apiSuccess (Success 200) {String} data.datas.payBehaviorCode  付款时间code 延后 提前
     * @apiSuccess (Success 200) {Integer} data.datas.payDayNumber  付款天数
     * @apiSuccess (Success 200) {String} data.datas.payDayTypeCode  付款时间类型 工作日，自然日，指定几号
     * @apiSuccess (Success 200) {String} data.datas.chargingTypeCode  计费类型 按月，按实际天数
     * @apiSuccess (Success 200) {String} data.datas.unitPrice_RoleCode  单价换算规则
     * @apiSuccess (Success 200) {String} data.datas.conversionDayCode  换算天数
     * @apiSuccess (Success 200) {Integer} data.datas.payCycle  付款周期
     * @apiSuccess (Success 200) {String} data.datas.leaseTermDivisionCode  租期划分方式
     * @apiSuccess (Success 200) {Integer} data.datas.status  状态 1正常 2删除
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":[]}
     */
    @RequestMapping(value = "/gets", method = RequestMethod.POST)
    @ApiOperation(value = "合同租金条款详情查询", httpMethod = "POST")
    public TerminusResult<List<ContractRentClauseResult>> gets(@RequestBody ContractRentClauseGetsForm form) {
        ContractRentClauseGetsParam param = BeanCopyUtils.copyProperties(form, ContractRentClauseGetsParam.class);
        try {
            return contractRentClauseService.gets(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("查询合同租金条款失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("查询合同租金条款失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    /**
     * @api {POST} /contractRentClause/query 合同租金条款查询
     * @apiVersion 1.0.0
     * @apiGroup ContractRentClause
     * @apiName 合同租金条款查询
     * @apiDescription 合同租金条款查询
     * @apiParam (RequestBody) {Long} contractId  合同ID
     * @apiParam (RequestBody) {Object} clauseBeginTime  条款开始时间
     * @apiParam (RequestBody) {Object} clauseEndTime  条款结束时间
     * @apiParam (RequestBody) {Double} rentalPrice  租金价格
     * @apiParam (RequestBody) {String} rentalUnitCode  租金单位code
     * @apiParam (RequestBody) {String} payBehaviorCode  付款时间code 延后 提前
     * @apiParam (RequestBody) {Integer} payDayNumber  付款天数
     * @apiParam (RequestBody) {String} payDayTypeCode  付款时间类型 工作日，自然日，指定几号
     * @apiParam (RequestBody) {String} chargingTypeCode  计费类型 按月，按实际天数
     * @apiParam (RequestBody) {String} unitPrice_RoleCode  单价换算规则
     * @apiParam (RequestBody) {String} conversionDayCode  换算天数
     * @apiParam (RequestBody) {Integer} payCycle  付款周期
     * @apiParam (RequestBody) {String} leaseTermDivisionCode  租期划分方式
     * @apiParam (RequestBody) {Integer} status  状态 1正常 2删除
     * @apiSuccess (Success 200) {Object} data    结果
     * @apiSuccess (Success 200) {Object[]} data.datas    合同租金条款列表
     * @apiSuccess (Success 200) {Long} data.datas.id  ID
     * @apiSuccess (Success 200) {Long} data.datas.contractId  合同ID
     * @apiSuccess (Success 200) {Object} data.datas.clauseBeginTime  条款开始时间
     * @apiSuccess (Success 200) {Object} data.datas.clauseEndTime  条款结束时间
     * @apiSuccess (Success 200) {Double} data.datas.rentalPrice  租金价格
     * @apiSuccess (Success 200) {String} data.datas.rentalUnitCode  租金单位code
     * @apiSuccess (Success 200) {String} data.datas.payBehaviorCode  付款时间code 延后 提前
     * @apiSuccess (Success 200) {Integer} data.datas.payDayNumber  付款天数
     * @apiSuccess (Success 200) {String} data.datas.payDayTypeCode  付款时间类型 工作日，自然日，指定几号
     * @apiSuccess (Success 200) {String} data.datas.chargingTypeCode  计费类型 按月，按实际天数
     * @apiSuccess (Success 200) {String} data.datas.unitPrice_RoleCode  单价换算规则
     * @apiSuccess (Success 200) {String} data.datas.conversionDayCode  换算天数
     * @apiSuccess (Success 200) {Integer} data.datas.payCycle  付款周期
     * @apiSuccess (Success 200) {String} data.datas.leaseTermDivisionCode  租期划分方式
     * @apiSuccess (Success 200) {Integer} data.datas.status  状态 1正常 2删除
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":[]}
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    @ApiOperation(value = "合同租金条款查询", httpMethod = "POST")
    public TerminusResult<List<ContractRentClauseResult>> query(@RequestBody ContractRentClauseQueryForm form) {
        ContractRentClauseQueryParam param = BeanCopyUtils.copyProperties(form, ContractRentClauseQueryParam.class);
        try {
            return contractRentClauseService.query(param, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("查询合同租金条款失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("查询合同租金条款失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

    /**
     * @api {POST} /contractRentClause/page 合同租金条款分页查询
     * @apiVersion 1.0.0
     * @apiGroup ContractRentClause
     * @apiName 合同租金条款分页查询
     * @apiDescription 合同租金条款分页查询
     * @apiParam (RequestBody) {Long} contractId  合同ID
     * @apiParam (RequestBody) {Object} clauseBeginTime  条款开始时间
     * @apiParam (RequestBody) {Object} clauseEndTime  条款结束时间
     * @apiParam (RequestBody) {Double} rentalPrice  租金价格
     * @apiParam (RequestBody) {String} rentalUnitCode  租金单位code
     * @apiParam (RequestBody) {String} payBehaviorCode  付款时间code 延后 提前
     * @apiParam (RequestBody) {Integer} payDayNumber  付款天数
     * @apiParam (RequestBody) {String} payDayTypeCode  付款时间类型 工作日，自然日，指定几号
     * @apiParam (RequestBody) {String} chargingTypeCode  计费类型 按月，按实际天数
     * @apiParam (RequestBody) {String} unitPrice_RoleCode  单价换算规则
     * @apiParam (RequestBody) {String} conversionDayCode  换算天数
     * @apiParam (RequestBody) {Integer} payCycle  付款周期
     * @apiParam (RequestBody) {String} leaseTermDivisionCode  租期划分方式
     * @apiParam (RequestBody) {Integer} status  状态 1正常 2删除
     * @apiSuccess (Success 200) {Object} data    结果
     * @apiSuccess (Success 200) {long} data.pageSize    分页大小
     * @apiSuccess (Success 200) {long} data.currentPage    当前页
     * @apiSuccess (Success 200) {long} data.totalPages    总页数
     * @apiSuccess (Success 200) {long} data.totalRows    总记录数
     * @apiSuccess (Success 200) {long} data.minRowNumber    当页起始行号
     * @apiSuccess (Success 200) {long} data.maxRowNumber    当页最大行号
     * @apiSuccess (Success 200) {Object[]} data.datas    合同租金条款列表
     * @apiSuccess (Success 200) {Long} data.datas.id  ID
     * @apiSuccess (Success 200) {Long} data.datas.contractId  合同ID
     * @apiSuccess (Success 200) {Object} data.datas.clauseBeginTime  条款开始时间
     * @apiSuccess (Success 200) {Object} data.datas.clauseEndTime  条款结束时间
     * @apiSuccess (Success 200) {Double} data.datas.rentalPrice  租金价格
     * @apiSuccess (Success 200) {String} data.datas.rentalUnitCode  租金单位code
     * @apiSuccess (Success 200) {String} data.datas.payBehaviorCode  付款时间code 延后 提前
     * @apiSuccess (Success 200) {Integer} data.datas.payDayNumber  付款天数
     * @apiSuccess (Success 200) {String} data.datas.payDayTypeCode  付款时间类型 工作日，自然日，指定几号
     * @apiSuccess (Success 200) {String} data.datas.chargingTypeCode  计费类型 按月，按实际天数
     * @apiSuccess (Success 200) {String} data.datas.unitPrice_RoleCode  单价换算规则
     * @apiSuccess (Success 200) {String} data.datas.conversionDayCode  换算天数
     * @apiSuccess (Success 200) {Integer} data.datas.payCycle  付款周期
     * @apiSuccess (Success 200) {String} data.datas.leaseTermDivisionCode  租期划分方式
     * @apiSuccess (Success 200) {Integer} data.datas.status  状态 1正常 2删除
     * @apiSuccessExample {json} 请求成功返回值:
     * {"code":"00000000","success":true,"message":"成功","data":[]}
     */
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiOperation(value = "合同租金条款分页查询", httpMethod = "POST")
    public TerminusResult<TerminusPageAble<ContractRentClauseResult>> page(@RequestBody ContractRentClauseQueryForm form) {
        ContractRentClauseQueryParam param = BeanCopyUtils.copyProperties(form, ContractRentClauseQueryParam.class);
        try {
            TerminusPageInfo page = new TerminusPageInfo(form.getCurrentPage(), form.getRowsOfPage());
            return contractRentClauseService.page(param, page, this.getTerminusContext());
        } catch (ServiceException e) {
            logger.error("分页查询合同租金条款失败，原因：", e);
            return TerminusResult.fail(e.getCode(), e.getMessage());
        } catch (Exception e) {
            logger.error("分页查询合同租金条款失败，原因：", e);
            //todo 修改错误码
            return TerminusResult.fail("", this.getMessage(""));
        }
    }

}
